package com.raidpoints.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.RowStyles;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.DockLayoutPanel;

public class EventTable extends Composite implements RaidPoints.HasRedraw{
	CellTable<event> eTable;
	ListHandler<event> sortHandler;
	SingleSelectionModel<event> selectionModel;
	Button btnAddEvent;
	RaidPoints rp;
	SimplePager simplePager;
	Comparator<event> tsComp;
	boolean noselect;
	Column<event,event> signCol;
	boolean showx;
	Button calButton;
	
	public EventTable(RaidPoints mrp) {
		rp = mrp;
		noselect = false;
		showx = false;
		sortHandler = new ListHandler<event>(rp.dataProvider.getList());
		selectionModel = new SingleSelectionModel<event>();
		
		DockLayoutPanel layoutPanel = new DockLayoutPanel(Unit.EM);
		initWidget(layoutPanel);
		
		LayoutPanel layoutPanel_1 = new LayoutPanel();
		layoutPanel.addNorth(layoutPanel_1,4.0);
		
		
		
		btnAddEvent = new Button("Add Event");
		btnAddEvent.setVisible(false);
		layoutPanel_1.add(btnAddEvent);
		layoutPanel_1.setWidgetLeftWidth(btnAddEvent, 231.0, Unit.PX, 98.0, Unit.PX);
		layoutPanel_1.setWidgetTopHeight(btnAddEvent, 13.0, Unit.PX, 28.0, Unit.PX);
		eTable = new CellTable<event>();
		eTable.setPageSize(25);
		eTable.setSize("100%", "100%");
		eTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		ScrollPanel scrollpan = new ScrollPanel();
		scrollpan.add(eTable);
		layoutPanel.add(scrollpan);
		rp.dataProvider.addDataDisplay(eTable);
		eTable.addColumnSortHandler(sortHandler);
		eTable.setSelectionModel(selectionModel);
		
		SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
		simplePager = new SimplePager(TextLocation.CENTER,pagerResources,false,0,true);
		simplePager.setDisplay(eTable);
		layoutPanel_1.add(simplePager);
		layoutPanel_1.setWidgetLeftWidth(simplePager, 15.0, Unit.PX, 210.0, Unit.PX);
		layoutPanel_1.setWidgetTopHeight(simplePager, 0.0, Unit.PX, 72.0, Unit.PX);
		
		calButton = new Button("Switch to Calendar");
		layoutPanel_1.add(calButton);
		layoutPanel_1.setWidgetLeftWidth(calButton, 359.0, Unit.PX, 143.0, Unit.PX);
		layoutPanel_1.setWidgetTopHeight(calButton, 13.0, Unit.PX, 28.0, Unit.PX);
		
		calButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				rp.CalSwitch(true);				
			}
			
		});
		

		TextColumn<event> startCol = new TextColumn<event>(){

			@Override
			public String getValue(event object) {
				return object.starttime.getFullDate(rp.mil);
				//return DateTimeFormat.getFormat("yyyy-MM-dd HH:mm").format(object.starttime,rp.pacTime);			
			}
			
		};
		eTable.addColumn(startCol,"Start");
		startCol.setSortable(true);
		sortHandler.setComparator(startCol, tsComp);
		
		TextColumn<event> endCol = new TextColumn<event>(){

			@Override
			public String getValue(event object) {
				return object.endtime.getFullDate(rp.mil);
			}
			
		};
		eTable.addColumn(endCol,"End");
		
		
		TextColumn<event> destCol = new TextColumn<event>(){

			@Override
			public String getValue(event object) {
				if(object.game.equals("")) return object.note.split(":;:")[0];
				else return object.dest;
			}
			
		};
		eTable.addColumn(destCol,"Destination");
		
		TextColumn<event> noteCol = new TextColumn<event>(){

			@Override
			public String getValue(event object) {
				if(object.game.equals("")) return object.note.split(":;:")[1];
				else return object.note;
			}
			
		};
		eTable.addColumn(noteCol,"Note");
		eTable.setColumnWidth(noteCol,"20em");
		
		TextColumn<event> gameCol = new TextColumn<event>(){

			@Override
			public String getValue(event object) {
				return object.game;
			}
			
		};
		eTable.addColumn(gameCol,"Game");
		
		//ActionCell<event> signcell = new ActionCell<event>("Signup",new ActionCell.Delegate<event>(){
		MyActionCell signcell = new MyActionCell("Signup",new ActionCell.Delegate<event>(){

			@Override
			public void execute(event object) {
				EventSignupBox sb = new EventSignupBox(rp,object);
				sb.center(); sb.show();
				noselect = true;
			}
			
		});
		
		signCol = new Column<event,event>(signcell){

			@Override
			public event getValue(event object) {
				return object;
			}
			
		};
		//eTable.addColumn(signCol,"");
		
				
		setSize("714px", "469px");
		
		btnAddEvent.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				NewEventBox nebox = new NewEventBox(rp);
				nebox.center(); nebox.show();
			}
			
		});
		
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
			public void onSelectionChange(SelectionChangeEvent event) {
				event selected = selectionModel.getSelectedObject();
				if(selected != null){
					if(noselect) noselect = false;
					else rp.ViewPoints(selected);
					selectionModel.setSelected(selectionModel.getSelectedObject(),false);
				}	
			}	
		});
		
		eTable.setRowStyles(new RowStyles<event>(){
			@Override
			public String getStyleNames(event row, int rowIndex) {
				if(row.attend.equals("slotted") || row.attend.equals("standby") || row.attend.equals("signedup")){
					return "signedup";
				}
				else return "";
			}
		});
		
	}
	
	public class MyActionCell extends ActionCell<event>{

		public MyActionCell(String text,
				com.google.gwt.cell.client.ActionCell.Delegate<event> delegate) {
			super(text, delegate);
			// TODO Auto-generated constructor stub
		}
		
		  @Override
		  public void render(Context context, event value, SafeHtmlBuilder sb) {
		    if(!value.over) super.render(context,value,sb);
		  }

		
	};

	public void HandleAuthChange(RaidPoints.AuthType atype){
		switch(atype){
		case admin:
			btnAddEvent.setVisible(true);
			if(!showx){
				eTable.addColumn(signCol,"");
				showx = true;
			}
			break;
		case cl:
			btnAddEvent.setVisible(true);
			if(!showx){
				eTable.addColumn(signCol,"");
				showx = true;
			}
			break;
		case user:
			btnAddEvent.setVisible(false);
			if(!showx){
				eTable.addColumn(signCol,"");
				showx = true;
			}
			break;
		default:
			btnAddEvent.setVisible(false);
			if(showx) {
				eTable.removeColumn(signCol);
				showx = false;
			}
			break;
		}
		
	}
	
	
	public void AddEvent(event evt){
		
	}

	@Override
	public void Redraw() {
		eTable.redraw();		
	}

	@Override
	public void HandleEvents() {
		eTable.setRowCount(rp.dataProvider.getList().size());
		simplePager.firstPage();
	}


	@Override
	public event GetEvent(int eid) {
		// TODO Auto-generated method stub
		return null;
	}
}
